#include "addleavedialog.h"
#include "ui_addleavedialog.h"
#include <QComboBox>
#include <QPushButton>
#include<QLineEdit>


AddLeaveDialog::AddLeaveDialog( MemberManage* mm, DayLeave *dl, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddLeaveDialog)
{
    ui->setupUi(this);
    this->mm = mm;
    this->dl = dl;

    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::red);
    ui->nameWarn->setPalette(pa);
    ui->reasonWarn->setPalette(pa);
    ui->timeWarn->setPalette(pa);
}

AddLeaveDialog::~AddLeaveDialog()
{
    delete ui;
}

void AddLeaveDialog::on_AddLeaveDialog_accepted()
{
    std::cout<<"open"<<std::endl;
}

void AddLeaveDialog::showEvent(QShowEvent *event){
    QComboBox & name = *(ui->nameBox);
    QComboBox & time = *(ui->timeBox);
    QLineEdit & reason= *(ui->reason);

    name.clear();
    time.clear();
    reason.clear();

    ui->timeWarn->setText("");
    ui->nameWarn->setText("");
    ui->reasonWarn->setText("");

    for(int i=0;i<mm->count();i++){
        name.addItem(QString::fromStdString(mm->getMember(i).name));
    }
    time.addItem(QString(MORNINGSTR));
    time.addItem(QString(AFTERSTR));
    time.addItem(QString(ALLSTR));
    name.setCurrentIndex(-1);
    time.setCurrentIndex(-1);


}



void AddLeaveDialog::on_buttonBox_clicked(QAbstractButton *btn)
{
    if(ui->buttonBox->button(QDialogButtonBox::Ok)==btn){
      //TODO
        QComboBox & name = *(ui->nameBox);
        QComboBox & time = *(ui->timeBox);
        QLineEdit & reason= *(ui->reason);

        std::set<int> check;

        if(name.currentIndex()==-1){
            ui->nameWarn->setText("选择请假人");
        }else{
            ui->nameWarn->setText("");
            check.insert(1);
        }

        if(reason.text().toStdString()==""){
            ui->reasonWarn->setText("填写请假事由");

        }else{
            ui->reasonWarn->setText("");
             std::cout<<reason.text().toStdString()<<std::endl;
            check.insert(2);
        }

        if(time.currentIndex()==-1){
            ui->timeWarn->setText("选择请假时长");
        }else{
            ui->timeWarn->setText("");
            check.insert(3);
        }

        if(check.count(1)==1&&check.count(2)==1&&check.count(3)==1){
            OneLeave temp{mm->getMember(name.currentIndex()).id,
                        mm->getMember(name.currentIndex()).name,
                        reason.text().toStdString(),
                        time.currentIndex()+1};
            if(!dl->addLeave(temp)){
                //TODO LOG

            }else{
                emit saveLeaves();
                this->close();
            }
        }

    }else {

    }
}

void AddLeaveDialog::closeEvent(QCloseEvent *event){
    event->accept();
}

void AddLeaveDialog::accept(){

}
